#include <netlib/net/IOCPManager.h>
#include <utillib/service/winservice.h>
#include <utillib/memory/Memory.h>
#include <netlib/session/sessionmanager.h>
#include <utillib/io/Registry.h>
#include "packet/gameserverpacketprocess.h"

utillib::service::WinService::ServiceProperty g_property;

netlib::session::Session *getSession();
netlib::net::IOCPManager g_iocp;

bool startingService()
{
	g_iocp.run( g_property.port, g_property.backLog, g_property.ip , g_property.useNagle , g_property.acceptThreadCount, g_property.workerThreadCount  );
#ifdef _DEBUG
	printf( "server running\r\n" );
#endif
	return ( true );
}

void stoppingService()
{
}

void main( int argc, char **argv )
{
	GameServerPacketProcess packetProcess;
	g_iocp.getSessionManager()->setPacketProcess( &packetProcess );
	g_property.port = 7008;
	g_property.ip = INADDR_ANY;
	g_property.backLog = 1024;
	g_property.acceptThreadCount = DEFAULT_ACCEPT_THREAD;
	g_property.workerThreadCount = 2;
	g_property.useNagle = true;
	strcpy_s( g_property.serviceName, "AriServer" );
	utillib::service::WinService::getInstance()->readConfig( "config.ini", &g_property );

	srand( ( DWORD )time ( NULL ) );

	utillib::service::WinService::getInstance()->setServiceFunction( startingService, stoppingService );
	utillib::service::WinService::getInstance()->setServiceName( g_property.serviceName);

	if ( argc == 2 )
	{
		if ( !_stricmp( argv[1],  "install" ) ) utillib::service::WinService::registerService( g_property.serviceName );
		if ( !_stricmp( argv[1],  "uninstall" ) ) utillib::service::WinService::removeService( g_property.serviceName );

		return;
	}

#ifdef _DEBUG
	SetConsoleCtrlHandler( utillib::service::WinService::breakHandler, TRUE );
	utillib::service::WinService::getInstance()->serviceMain( 0, NULL );

	SetConsoleCtrlHandler( utillib::service::WinService::breakHandler, FALSE );
#else
	utillib::service::WinService::getInstance()->serviceDispatcher();
#endif

	g_iocp.stopAcceptor();
	return;
}
